<?php

namespace App\Http\Middleware;

use App\Logic\Auth\WebAuthLogic;
use App\Models\AdminUserModel;
use Closure;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Auth;

class CheckRole
{
    private Route $route;
    function __construct(Route $route)
    {
        $this->route = $route;
    }

    /**
     * @param Request $request
     * @param Closure $next
     * @return void
     * @throws AuthenticationException
     */
    public function handle(Request $request, Closure $next)
    {
        $route_name = $this->route->getName();
        if ($this->route->uri() === 'sanctum/csrf-cookie') {
            return $next($request);
        }

        if ((new WebAuthLogic)->check($route_name) === false) {
            throw new AuthenticationException('没有权限访问');
        }
        return $next($request);
    }
}
